//
//  CallBackNumberCell.swift
//  d11
//
//  Created by peale_pro on 2019/10/16.
//  Copyright © 2019 Tech_001. All rights reserved.
//

import UIKit
typealias NumberChangeBlock = (_ number: String) -> Void
class CallBackNumberCell: UITableViewCell,UITextFieldDelegate {
    var numberField : UITextField?
    var numberChangeBlock: NumberChangeBlock?
      func callChangeBlock(_ block: @escaping NumberChangeBlock) {
          numberChangeBlock = block
      }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.setUpUI()

    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.numberField = UITextField.init()
        self.numberField?.textColor =  HexRGBAlpha(0x8E8F90, 1)
        self.numberField?.font = UIFont.systemFont(ofSize: 15)
        self.numberField?.textAlignment = NSTextAlignment.left
        self.numberField?.keyboardType  = .numberPad
        self.numberField?.placeholder = "输入联系电话号码"
        self.numberField?.delegate = self
        self.numberField?.borderStyle = .roundedRect
        
        let leftView = UIView.init()
        leftView.frame = CGRect(x: 0, y: 0, width: 30, height: 15)
        
        let leftImgView = UIImageView(image: UIImage.init(named: "icon_kefu_submit_phone"))
        leftImgView.frame = CGRect(x: 10, y: 0, width: 15, height: 15)
        leftImgView.contentMode = .center
        leftView.addSubview(leftImgView)
        numberField?.leftView = leftView
        numberField?.leftViewMode = .always

        self.contentView.addSubview(self.numberField!)
        self.numberField?.addTarget(self, action: #selector(self.actionToChanged(_:)), for: UIControl.Event.editingChanged)
        self.numberField?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.top.equalToSuperview()
            make.height.equalTo(45)
        }
    }
    
    @objc func actionToChanged(_ sender: UITextField) -> Void {
        
        if numberChangeBlock != nil {
            numberChangeBlock!(sender.text!)
        }
    }

}
